package com.dayatang.hrm.compensation.infra.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.hibernate.HibernateSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayatang.commons.repository.EntityRepositoryHibernateTapestry;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuite;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuiteSetting;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuiteSettingRepository;
import com.dayatang.hrm.organisation.domain.Organization;

@SuppressWarnings("unchecked")
public class SalaryAccountSuiteSettingRepositoryImpl extends
		EntityRepositoryHibernateTapestry<SalaryAccountSuiteSetting, Long>
		implements SalaryAccountSuiteSettingRepository {

	private static final Logger logger = LoggerFactory
			.getLogger(SalaryAccountSuiteSettingRepositoryImpl.class);

	public SalaryAccountSuiteSettingRepositoryImpl(
			HibernateSessionManager sessionManager) {
		super(SalaryAccountSuiteSetting.class, sessionManager);
	}

	@Override
	public SalaryAccountSuiteSetting get(SalaryAccountSuite accountSuite,
			Date queryDate) {
		List<SalaryAccountSuiteSetting> list = getSessionManager().getSession()
				.getNamedQuery("SalaryAccountSuiteSetting.get").setEntity(
						"pSuite", accountSuite).setTimestamp("pQueryDate",
						queryDate).list();

		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public SalaryAccountSuiteSetting getPure(
			SalaryAccountSuiteSetting salaryAccountSuiteSetting) {
		getSessionManager().getSession().evict(salaryAccountSuiteSetting);
		return get(salaryAccountSuiteSetting.getId());
	}

	@Override
	public boolean isNameAlreadyUsedInSameOrganizationExceptSelf(String name,
			Organization org, Date fromDate, Date toDate,
			SalaryAccountSuiteSetting selfSetting) {

		boolean isDuplicate = false;
		String comparedName = name.trim();
		List<SalaryAccountSuiteSetting> settings = new ArrayList<SalaryAccountSuiteSetting>();

		if (selfSetting == null) {
			settings = getSessionManager()
					.getSession()
					.getNamedQuery(
							"SalaryAccountSuiteSetting.isNameAlreadyUsedInSameOrganizationExceptSelf.selfIsNull")
					.setEntity("pOrg", org).setTimestamp("pFromDate", fromDate)
					.setTimestamp("pToDate", toDate).list();
		} else {
			settings = getSessionManager()
					.getSession()
					.getNamedQuery(
							"SalaryAccountSuiteSetting.isNameAlreadyUsedInSameOrganizationExceptSelf.selfIsExist")
					.setEntity("pOrg", org).setEntity("pSelf", selfSetting)
					.setTimestamp("pFromDate", fromDate).setTimestamp(
							"pToDate", toDate).list();
		}

		if (settings.isEmpty()) {
			return isDuplicate;
		}

		for (SalaryAccountSuiteSetting setting : settings) {

			if (logger.isDebugEnabled()) {
				logger
						.debug(
								"机构【{}】在时间段【{}至 {}】有效的薪资帐套设置信息为：name = {}， fromDate = {}，toDate = {}",
								new Object[] { org.getName(), fromDate, toDate,
										setting.getName(),
										setting.getFromDate(),
										setting.getToDate() });
			}

			String existedName = setting.getName();

			isDuplicate = existedName.equals(comparedName);

			if (isDuplicate)
				return isDuplicate;

		}

		return isDuplicate;

	}
}
